package com.s10.ecloud.web.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s10.ecloud.web.Constants;
import com.s10.ecloud.web.dao.IRoleDAO;
import com.s10.ecloud.web.dao.IUserDAO;
import com.s10.ecloud.web.model.EnterpriseUser;
import com.s10.ecloud.web.model.IndividualUser;
import com.s10.ecloud.web.model.Role;
import com.s10.ecloud.web.model.SystemUser;
import com.s10.ecloud.web.model.User;
import com.s10.ecloud.web.service.IUserService;

@Service
public class UserServiceImpl<U extends User> implements IUserService<U> {

	@Resource
	private IUserDAO<U> userDAO;
	@Resource
	private IRoleDAO roleDAO;

	public IUserDAO<U> getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO<U> userDAO) {
		this.userDAO = userDAO;
	}

	public String userLogin(U user) {

		return userDAO.userLogin(user);

	}

	public int userRegister(SystemUser user, IndividualUser userinfo, int depid) {
		if (userDAO.existUser(user.getName())) {

			return 2;

		} else {

			// Dep dep=depDAO.findDepbyId(depid);
			// userDAO.addUser(user);
			// userDAO.addUserinfo(user, userinfo,dep);

			return 0;
		}

	}

	public SystemUser findUserbyUsername(String username) {

		SystemUser user = userDAO.findUserbyUsername(username);
		return user;

	}

	@SuppressWarnings("unchecked")
	public U getUser(U user) {

		// User user=userDAO.findUserbyUsername(username);
		return (U) userDAO.findUserbyId(user);

	}

	@SuppressWarnings("unchecked")
	public List<U> findAllUser() {
		return (List<U>) userDAO.findAllUser();
	}

	public void freezeUserstateByUsername(String username) {

		userDAO.freezeUserstateByUsername(username);

	}

	public void recoverUserstateByUsername(String username) {

		userDAO.recoverUserstateByUsername(username);

	}

	public void modifyUserRights(String username, Role right) {

		userDAO.updateUserRole(username, right);

	}

	public String updatePassword(int userId, String password) {

		return userDAO.updatePassword(userId, password);

	}

	@SuppressWarnings("unchecked")
	public List<U> findAllUserByDep(SystemUser user) {

		return (List<U>) userDAO.findAllUserByDep();

	}

	@SuppressWarnings("unchecked")
	public List<U> findUsersByRight(Role right) {

		return (List<U>) userDAO.findUsersByRole(right);

	}

	public User updateUser(User user) {
		return userDAO.updateUser(user);
	}

	@Override
	public String userLogin(SystemUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IndividualUser findUserinfo(SystemUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modifyUserinfo(String name, String gender, String email,
			String pid, String phone, SystemUser user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modifyUserRights(String username, String right) {
		// TODO Auto-generated method stub

	}

	@Override
	public String modifyUserPwd(int userId, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List findAllUserByRight(String right) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public U addUser(U user) {

		userDAO.addUser(user);
		return this.getUser(user);
	}

	@Override
	public int getUserCountByEmail(String eMail) {
		return userDAO.findUserByEMail(eMail).size();
	}

	@Override
	public User updateActiveUser(int id, String personalID) {

		// 首先从三张用户表中去检索，记录
		User user = new IndividualUser();
		user.setId(id);

		user = userDAO.findUserbyId(user);

		if (user == null) {
			user = new EnterpriseUser();
			user.setId(id);

			user = userDAO.findUserbyId(user);

			if (user == null) {
				user = new SystemUser();
				user.setId(id);

				user = userDAO.findUserbyId(user);
			}
		}

		// 取到记录后，根据记录的角色，进行激活设定
		if (user != null && user.getPersonalId().equals(personalID)) {

			if (user.getRole().getId() == Constants.USER_TYPE_ENT) {
				// 企业用户的场合，设成待审批状态
				user.setState(Constants.USER_ACTIVE_STATE_WAITING_AUDIT);
			} else {
				// 个人用户时，直接激活。
				user.setState(Constants.USER_ACTIVE_STATE_VALID);
			}
			userDAO.updateUser(user);

			return user;
		}

		return null;
	}

	@Override
	public List<User> getEntUserByCommerceNo(String commerceNo) {
		return userDAO.getEntUserByCommerceNo(commerceNo);
	}

	@Override
	public Role getRoleById(int id) {
		return roleDAO.getRoleById(id);
	}

}
